#include "UsrAI.h"
#include<set>
#include<cmath>
#include <iostream>
#include<unordered_map>
#include<queue>
#define NUMBER_FARMER 17
#define NUMBER_HUTTINGFARMER 2
#define NUMBER_CUTTINGFARMER 13
#define NUMBER_BUILDINGFARMER_FARM 5
#define NUMBER_BUILDINGFARMER_DOCK 1
#define NUMBER_BUILDINGFARMER_HOME 1
#define NUMBER_BUILDINGFARMER_MARKET 1
#define NUMBER_BUILDINGFARMER_GRANARY 1
#define NUMBER_BUILDINGFARMER_STOCK 1
#define NUMBER_BUILDINGFARMER_STABLE 1
#define NUMBER_BUILDINGFARMER_RANGE 1
#define NUMBER_BUILDINGFARMER_ARMYCAMP 2
#define NUMBER_FISHINGFARMER 4
#define NUMBER_ARMYCAMP 2
#define NUMBER_RANGE 1
#define NUMBER_STABLE 1
#define NUMBER_GANARY 1
#define NUMBER_MARKET 1
#define NUMBER_FARM 5
#define NUMBER_DOCK 1
#define NUMBER_HOME 4
#define NUMBER_STOCK 2
#define NUMBER_SAILING 4
#define NUMBER_BOAT 1
//army number
#define NUMBER_SHIP 7
#define NUMBER_CLUBMAN 6
#define NUMBER_SCOUT 2
#define NUMBER_BOWMAN 3
#define NUMBER_SLINGER 3
#define DEADBLOOD 0.1
using namespace std;
tagGame tagUsrGame;
ins UsrIns;
/*##########MY CODE BEGINS HERE##########*/
static int frameCount = 0;
static bool hasGoldScience = false;
static bool hasUsetoolScience = false;
static bool hasDefenseInScience = false;
static bool hasDefenseArScience = false;
static bool hasDefenseRiScience = false;
static bool hasClubmanScience = false;
static bool hasFarmScience = false;
static bool hasWoodScience = false;
static bool hasStoneScience = false;
static int stage = 1;
tagInfo info;
static bool makeLandWar = false;
static bool makeOceanWar = false;
static pair<int,int> boardPos = {-1,-1};
static pair<int,int> landPos = {-1,-1};
char myMap[128][128];
map<int,int> SN_to_Sort;
map<int,int> Type_to_PreType;
map<int,int> Science_to_Building;
vector<pair<int,int>> directions = { {0,1}, {0,-1}, {-1,0}, {1,0} };
vector<tagFarmer> enermyLandIdleFarmer;
vector<tagFarmer> landIdleFarmer;
vector<tagFarmer> waterIdleFarmer;
vector<tagFarmer> cuttingFarmer;
vector<tagFarmer> fishingFarmer;
vector<tagFarmer> huttingFarmer;
vector<int> boardFarmerSN;
vector<tagFarmer> boatFarmer;
vector<tagFarmer> diggingGoldFarmer;
vector<tagFarmer> buildingStableFarmer;
vector<tagFarmer> buildingRangeFarmer;
vector<tagFarmer> buildingArmyCampFarmer;
vector<tagFarmer> buildingStockFarmer;
vector<tagFarmer> buildingFarmFarmer;
vector<tagFarmer> buildingHomeFarmer;
vector<tagFarmer> buildingDockFarmer;
vector<tagFarmer> buildingMarketFarmer;
vector<tagFarmer> buildingGranaryFarmer;
vector<tagArmy> armyShip;
vector<tagArmy> armyClubman;
vector<tagArmy> armyBowman;
vector<tagArmy> armyScout;
vector<tagArmy> armySlinger;
tagBuilding CENTER;
//MY FUNCTION
int getNumberBuilding(int BUILDING_TYPE){
    int count = 0;
    for(tagBuilding&building:info.buildings){
        if(building.Type==BUILDING_TYPE) count++;
    }
    return count;
}
int getNumberFarmer(int FARMER_SORT,bool enemyFlag = false){
    int count = 0;
    for(tagFarmer&farmer:info.farmers){
        if(farmer.FarmerSort==FARMER_SORT){
            if(FARMER_SORT==0){
                if(enemyFlag&&!isNearCenter(farmer.BlockDR,farmer.BlockUR)){
                    count++;
                }
                else if(!enemyFlag&&isNearCenter(farmer.BlockDR,farmer.BlockUR)){
                    count++;
                }
            }
            else{
                count++;
            }
        }
    }
    return count;
}
int getNumberResource(int RESOURCE_TYPE){
    int count = 0;
    for(tagResource&resource:info.resources){
        if(resource.Type==RESOURCE_TYPE) count++;
    }
    return count;
}
int getNumberArmy(int ARMY_TYPE){
    int count = 0;
    for(tagArmy&army:info.armies){
        if(army.Sort==ARMY_TYPE){
            count++;
        }
    }
    return count;
}
bool isLand(int DR,int UR){
    if(myMap[DR][UR]>='0'&&myMap[DR][UR]<='9') return true;
    else return false;
}
bool isOcean(int DR,int UR){
    if(myMap[DR][UR]=='-') return true;
    else return false;
}
bool isSlope(int DR, int UR) {
    if (!isLand(DR, UR)) return false;
    char height = myMap[DR][UR];
    // 检查周围的八个方向：上下左右 + 四个对角线
    int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
    int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
    for (int i = 0; i < 8; i++) {
        int nx = DR + dx[i];
        int ny = UR + dy[i];
        if (!isValidCoordinate(nx, ny)) continue;
        if (isLand(nx, ny) && myMap[nx][ny] != height) return true;
    }
    return false;
}
bool isSuitBuild(int DR, int UR, int BUILDING_TYPE,bool enermyFlag = false) {
    if( enermyFlag && isNearCenter(DR,UR) ) return false;
    char height = myMap[DR][UR];
    int wide = 0;
    if (BUILDING_TYPE == BUILDING_DOCK || BUILDING_TYPE == BUILDING_ARROWTOWER || BUILDING_TYPE == BUILDING_HOME) {
        wide = 2;
    }
    else{
        wide = 3;
    }
        for (int i = 0; i < wide; i++) {
            for (int j = 0; j < wide; j++) {
                int nx = DR + i;
                int ny = UR + j;
                if (!isValidCoordinate(nx, ny)) return false; // 越界控制
                if (!isLand(nx, ny) && !isOcean(nx, ny)) return false; // 非土地非海洋
                if (myMap[nx][ny] != height || isSlope(nx, ny)) return false;
            }
        }

        if (BUILDING_TYPE == BUILDING_DOCK) {
            // 码头必须在海洋上
            if (!isOcean(DR, UR)) return false;
            // 判断四周是否靠岸
            if (isLand(DR - 1, UR) && isLand(DR - 1, UR + 1)) return true;
            if (isLand(DR + 2, UR) && isLand(DR + 2, UR + 1)) return true;
            if (isLand(DR, UR - 1) && isLand(DR + 1, UR - 1)) return true;
            if (isLand(DR, UR + 2) && isLand(DR + 1, UR + 2)) return true;
            return false;
        }
        else {
            // 其他建筑必须在陆地上
            if (isLand(DR, UR)) return true;
            return false;
        }
    }
}
bool isEnermyLand(int DR, int UR){
    if(isLand(DR,UR)&&!isNearCenter(DR,UR)) return true;
    else return false;
}
void UsrAI::initFarmer(){
    enermyLandIdleFarmer.clear();
    huttingFarmer.clear();
    landIdleFarmer.clear();
    waterIdleFarmer.clear();
    cuttingFarmer.clear();
    fishingFarmer.clear();
    boatFarmer.clear();
    diggingGoldFarmer.clear();
    buildingStableFarmer.clear();
    buildingArmyCampFarmer.clear();
    buildingRangeFarmer.clear();
    buildingStockFarmer.clear();
    buildingDockFarmer.clear();
    buildingHomeFarmer.clear();
    buildingFarmFarmer.clear();
    buildingGranaryFarmer.clear();
    buildingMarketFarmer.clear();
    for(tagFarmer&farmer:info.farmers){
        bool inBoardFarmer = false;
        for(auto farmerSN:boardFarmerSN){
            if(farmer.SN==farmerSN){
                inBoardFarmer = true;
                break;
            }
        }
        if(inBoardFarmer) continue;
        //land
        if(farmer.FarmerSort==0){
            if(farmer.NowState==HUMAN_STATE_IDLE){
                if(isNearCenter(farmer.BlockDR,farmer.BlockUR)){
                    landIdleFarmer.push_back(farmer);
                }
                else{
                    enermyLandIdleFarmer.push_back(farmer);
                }
            }
            else if(farmer.NowState==HUMAN_STATE_WORKING){
                //第三阶段前，敌方大陆的村民的工作只允许为建筑
                if(!isNearCenter(farmer.BlockDR,farmer.BlockUR)&&stage<=3){
                   int type = SN_to_Sort[farmer.WorkObjectSN];
                   if(type!=BUILDING_ARMYCAMP&&type!=BUILDING_RANGE&&type!=BUILDING_STABLE){
                       HumanMove(farmer.SN,farmer.DR,farmer.UR);
                       continue;
                   }
                }
                if(SN_to_Sort[farmer.WorkObjectSN]==RESOURCE_TREE){
                    cuttingFarmer.push_back(farmer);
                }
                else if(SN_to_Sort[farmer.WorkObjectSN]==BUILDING_HOME){
                    buildingHomeFarmer.push_back(farmer);
                }
                else if(SN_to_Sort[farmer.WorkObjectSN]==BUILDING_DOCK){
                    buildingDockFarmer.push_back(farmer);
                }
                else if(SN_to_Sort[farmer.WorkObjectSN]==RESOURCE_GAZELLE){
                    huttingFarmer.push_back(farmer);
                }
                else if(SN_to_Sort[farmer.WorkObjectSN]==BUILDING_GRANARY){
                    buildingGranaryFarmer.push_back(farmer);
                }
                else if(SN_to_Sort[farmer.WorkObjectSN]==BUILDING_MARKET){
                    buildingMarketFarmer.push_back(farmer);
                }
                else if(SN_to_Sort[farmer.WorkObjectSN]==BUILDING_FARM){
                    buildingFarmFarmer.push_back(farmer);
                }
                else if(SN_to_Sort[farmer.WorkObjectSN]==BUILDING_STOCK){
                    buildingStockFarmer.push_back(farmer);
                }
                else if(SN_to_Sort[farmer.WorkObjectSN]==BUILDING_ARMYCAMP){
                    buildingArmyCampFarmer.push_back(farmer);
                }
                else if(SN_to_Sort[farmer.WorkObjectSN]==BUILDING_RANGE){
                    buildingRangeFarmer.push_back(farmer);
                }
                else if(SN_to_Sort[farmer.WorkObjectSN]==BUILDING_STABLE){
                    buildingStableFarmer.push_back(farmer);
                }
                else if(SN_to_Sort[farmer.WorkObjectSN]==RESOURCE_GOLD){
                    diggingGoldFarmer.push_back(farmer);
                }
            }
        }
        //water
        else if(farmer.FarmerSort==1){
            boatFarmer.push_back(farmer);
        }
        else if(farmer.FarmerSort==2){
            if(farmer.NowState==HUMAN_STATE_IDLE){
                waterIdleFarmer.push_back(farmer);
            }
            else if(farmer.NowState==HUMAN_STATE_WORKING)
            {
                if(SN_to_Sort[farmer.WorkObjectSN]==RESOURCE_FISH){
                    fishingFarmer.push_back(farmer);
                }
            }
        }
    }
}
void UsrAI::init(){
    //确定映射
    for(tagBuilding&building:info.buildings){
        SN_to_Sort[building.SN] = building.Type;
    }
    for(tagResource&resource:info.resources){
        SN_to_Sort[resource.SN] = resource.Type;
    }
    
    // 初始化建筑前置条件映射
    Type_to_PreType[BUILDING_FARM] = BUILDING_MARKET;
    Type_to_PreType[BUILDING_MARKET] = BUILDING_GRANARY;
    Type_to_PreType[BUILDING_RANGE] = BUILDING_ARMYCAMP;
    Type_to_PreType[BUILDING_STABLE] = BUILDING_ARMYCAMP;
    

    //找城镇中心
    for(tagBuilding&building:info.buildings){
        if(building.Type==BUILDING_CENTER){
            CENTER = building;
        }
    }
    if(info.armies.size()>=14) makeLandWar = true;
    if(getNumberArmy(AT_SHIP)>=7) makeOceanWar = true;
    initMap();
    initArmy();
    initFarmer();
}
void initArmy(){
    armyBowman.clear();
    armyScout.clear();
    armyClubman.clear();
    armySlinger.clear();
    armyShip.clear();
    for(tagArmy&army:info.armies){
        switch (army.Sort) {
        case AT_SHIP:
            armyShip.push_back(army);
            break;
        case AT_BOWMAN:
            armyBowman.push_back(army);
            break;
        case AT_CLUBMAN:
            armyClubman.push_back(army);
            break;
        case AT_SCOUT:
            armyScout.push_back(army);
            break;
        default:
            armySlinger.push_back(army);
            break;
        }
    }
}
void initMap(){
    //遍历地图
    for(int i=0;i<128;i++){
        for(int j=0;j<128;j++){
            tagTerrain terrain = (*info.theMap)[i][j];
            switch (terrain.type) {
            case MAPPATTERN_GRASS:
                myMap[i][j]=terrain.height+'0';
                break;
            case MAPPATTERN_OCEAN:
                myMap[i][j]='-';
                break;
            case MAPPATTERN_UNKNOWN:
                myMap[i][j]='?';
                break;
            }
        }
     }
    //遍历资源
    for(tagResource&resource:info.resources){
        int DR = resource.BlockDR;
        int UR = resource.BlockUR;
        if(resource.Type==RESOURCE_FISH||resource.Type==RESOURCE_GOLD||resource.Type==RESOURCE_STONE){
            for(int i=0;i<2;i++){
                for(int j=0;j<2;j++){
                    int nx = DR + i;
                    int ny = UR + j;
                    if(isValidCoordinate(nx,ny)){
                        myMap[nx][ny] = (resource.Type%22)+'A';
                    }
                }
            }
        }
        else{
            if(isValidCoordinate(DR,UR)){
                myMap[DR][UR] = (resource.Type%22)+'A';
            }
        }
    }
    //遍历建筑
    for(tagBuilding&building:info.buildings){
        int DR = building.BlockDR;
        int UR = building.BlockUR;
        if(building.Type==BUILDING_HOME||building.Type==BUILDING_ARROWTOWER||building.Type==BUILDING_DOCK){
            for(int i=0;i<2;i++){
                for(int j=0;j<2;j++){
                    int x = DR + i;
                    int y = UR + j;
                    if(isValidCoordinate(x,y)){
                        myMap[x][y] = (building.Type%26)+'a';
                    }
                }
            }
        }
        else{
            for(int i=0;i<3;i++){
                for(int j=0;j<3;j++){
                    int x = DR + i;
                    int y = UR + j;
                    if(isValidCoordinate(x,y)){
                        myMap[x][y] = building.Type+'a';
                    }
                }
            }
        }
    }
    //遍历单位
    for(tagFarmer&farmer:info.farmers){
        if(isValidCoordinate(farmer.BlockDR,farmer.BlockUR)){
            myMap[farmer.BlockDR][farmer.BlockUR] = '*';
        }
    }
    for(tagArmy&army:info.armies){
        if(isValidCoordinate(army.BlockDR,army.BlockUR)){
            myMap[army.BlockDR][army.BlockUR] = '*';
        }
    }
}
pair<int,int> findLandPos(){
    pair<int,int> res = {-1,-1};
    if(boardPos.first==-1) return res;
    
    int disMin = INT_MAX;
    for(int i=0;i<128;i++){
        for(int j=0;j<128;j++){
            // 寻找地方海岸线(避免锯齿状)
            if(!isOcean(i,j)) continue;
            bool nearEnemyLand = false;
            for(auto p:directions){
                int nx = p.first+i;
                int ny = p.second+j;
                if(isValidCoordinate(nx,ny)&&isEnermyLand(nx,ny)){
                    nearEnemyLand = true;
                    break;
                }
            }
            if(!nearEnemyLand) continue;
            int dis = abs(i-boardPos.first)+abs(j-boardPos.second);
            if(dis<disMin){
                res = {i,j};
                disMin = dis;
            }
        }
    }    
    return res;
}
int  findNearestResource(const tagFarmer& farmer,int RESOURCE_TYPE,bool enemyFlag = false){
    bool visit[128][128];
    memset(visit,0,sizeof(visit));
    char target = (RESOURCE_TYPE%22)+'A';
    queue<pair<int,int>> q;
    pair<int,int> res;
    pair<int,int> farmer_pos = {farmer.BlockDR,farmer.BlockUR};
    visit[farmer_pos.first][farmer_pos.second]=1;
    q.push(farmer_pos);
    while(!q.empty()){
        pair<int,int> current = q.front();
        q.pop();
        int x = current.first;
        int y = current.second;
        if(myMap[x][y]==target){
            if(enemyFlag&&!isNearCenter(x,y)){
                res = current;
                break;
            }
            else if(!enemyFlag&&isNearCenter(x,y)){
                res = current;
                break;
            }
        }
        for(const auto& p:directions){
            int nx = x+p.first;//now x
            int ny = y+p.second;//now y
            if(isValidCoordinate(nx,ny) && !visit[nx][ny]){
                    q.push({nx,ny});
                    visit[nx][ny] = 1;
            }
        }
    }
    bool flag = (RESOURCE_TYPE==RESOURCE_FISH||RESOURCE_TYPE==RESOURCE_GOLD||RESOURCE_TYPE==RESOURCE_STONE);
    for(tagResource&resource:info.resources){
        if(resource.Type==RESOURCE_TYPE){
            if(flag){
                for(int i=0;i<2;i++){
                    for(int j=0;j<2;j++){
                        int nx = resource.BlockDR+i;
                        int ny = resource.BlockUR+j;
                        if(nx==res.first&&ny==res.second) return resource.SN;
                    }
                }
            }
            else{
                if(resource.BlockDR==res.first&&resource.BlockUR==res.second) return resource.SN;
            }
        }
    }
    return -1;
}
pair<int, int> searchDirection(int begin_X, int begin_Y, int dx, int dy, bool flag, int BUILDING_TYPE,bool enermyFlag) {
    // 预定义农场位置，避免重复计算
    static const vector<pair<int, int>> FARM_POSITIONS = {
        {0, 4}, {4, 3}, {4, 0}, {4, -3},
        {0, -4}, {-4, 0}, {-4, 3}, {-4, -3}
    };

    if (flag) {
        // 建筑搜索逻辑
        for (int i = 0; ; i += dx) {
            int x = begin_X + i;
            if (!isValidCoordinate(x, 0)) break;

            for (int j = 0; ; j += dy) {
                int y = begin_Y + j;
                if (!isValidCoordinate(0, y)) break;

                // 跳过排除区域
                if ((dx > 0 && i <= 7 && dy > 0 && j <= 7) ||
                    (dx > 0 && i <= 7 && dy < 0 && j >= -5) ||
                    (dx < 0 && i >= -5 && dy > 0 && j <= 7) ||
                    (dx < 0 && i >= -5 && dy < 0 && j >= -5)) {
                    continue;
                }

                // 检查是否适合建造
                if (isSuitBuild(x, y, BUILDING_TYPE,enermyFlag)) {
                    return {x, y};
                }
            }
        }
    } else {
        // 农场搜索逻辑
        for (const auto& offset : FARM_POSITIONS) {
            int x = begin_X + offset.first;
            int y = begin_Y + offset.second;

            if (isValidCoordinate(x, y) && isLand(x, y)) {
                return {x, y};
            }
        }
    }

    return {-1, -1};
}
pair<int, int> findBuildingBlock(int BUILDING_TYPE,bool enermyFlag = false) {
    pair<int, int> res = {-1, -1};
    bool flag = (BUILDING_TYPE != BUILDING_FARM);
    int begin_X,begin_Y;
    if(enermyFlag){
        begin_X = landPos.first;
        begin_Y = landPos.second;
    }
    else{
        begin_X = CENTER.BlockDR;
        begin_Y = CENTER.BlockUR;
    }
    // 四个方向：右下、左下、右上、左上
    pair<int, int> directions[4] = { {1, 1}, {1, -1}, {-1, 1}, {-1, -1} };
    for (auto direction:directions) {
        res = searchDirection(begin_X, begin_Y, direction.first, direction.second, flag, BUILDING_TYPE,enermyFlag);
        if (res.first != -1) return res;
    }
    return res;
}
int totalDistance(int dr, int ur,int RESOURCE_TYPE) {
    int sum = 0;
    for (tagResource&resource:info.resources) {
        if(resource.Type==RESOURCE_TYPE){
            sum+=abs(dr-resource.BlockDR)+abs(ur-resource.BlockUR);
        }
    }
    return sum;
}
pair<int, int> findBestPosition(int BUILDING_TYPE,int RESOURCE_TYPE,bool enemyFlag = false) {
    int bestDist = INT_MAX;
    pair<int, int> bestPos = {-1, -1};
    for (int x = 0; x < 128; ++x) {
        for (int y = 0; y < 128; ++y) {
            if (!isSuitBuild(x, y, BUILDING_TYPE,enemyFlag)) continue;
            int dist = totalDistance(x, y, RESOURCE_TYPE);
            if (dist < bestDist) {
                bestDist = dist;
                bestPos = {x, y};
            }
        }
    }
    return bestPos;
}
void UsrAI::assignBuildTask(int BUILDING_TYPE, vector<tagFarmer>& idleFarmer, vector<tagFarmer>& workingFarmer, int workingNum, pair<int,int> goal, int BUILDING_RESOURCE, int buildingNum){
    // 检查前置条件
    if(!isPreBuild(BUILDING_TYPE)) return;
    
    // 检查资源是否足够
    if(BUILDING_TYPE == BUILDING_ARROWTOWER && info.Stone < BUILDING_RESOURCE) return;
    if(BUILDING_TYPE != BUILDING_ARROWTOWER && info.Wood < BUILDING_RESOURCE) return;
    
    // 分配建造任务
    while(getNumberBuilding(BUILDING_TYPE) < buildingNum && !idleFarmer.empty() && workingFarmer.size() < workingNum){
        if(goal.first == -1) return; // 没有合适的建造位置
        
        tagFarmer farmer = idleFarmer.back();
        HumanBuild(farmer.SN, BUILDING_TYPE, goal.first, goal.second);
        //实时更新地图
        int wide = 0;
        if (BUILDING_TYPE == BUILDING_DOCK || BUILDING_TYPE == BUILDING_ARROWTOWER || BUILDING_TYPE == BUILDING_HOME){
            wide = 2;
        }
        else{
            wide = 3;
        }
        for(int dx=0;dx<wide;dx++){
            for(int dy=0;dy<wide;dy++){
                int nx = goal.first + dx;
                int ny = goal.second+ dy;
                myMap[nx][ny] = (BUILDING_TYPE%26)+'a';
            }
        }
        idleFarmer.pop_back();
        workingFarmer.push_back(farmer);
    }
}
void UsrAI::assignResourceTask(int RESOURCE_TYPE, vector<tagFarmer>& idleFarmer, vector<tagFarmer>& workingFarmer, int workingNum,bool enemyFlag = false){
    if(getNumberResource(RESOURCE_TYPE)==0) return;
    if(RESOURCE_TYPE==RESOURCE_FISH){
        for(tagFarmer&farmer:workingFarmer){
            int goalSN=findNearestResource(farmer,RESOURCE_TYPE,enemyFlag);
            if(farmer.WorkObjectSN!=goalSN){
                HumanAction(farmer.SN,goalSN);
            }
        }
    }
    while(idleFarmer.size() && workingFarmer.size()<workingNum){
        tagFarmer farmer = idleFarmer.back();
        int goalSN = findNearestResource(farmer,RESOURCE_TYPE);
        if(goalSN!=-1){
            HumanAction(farmer.SN,goalSN);
            idleFarmer.pop_back();
            workingFarmer.push_back(farmer);
        }
        else{
            return;
        }
    }
}
void UsrAI::huttingGazelle() {
    //return if no gazelle
    if(!getNumberResource(RESOURCE_GAZELLE)) return;

    assignResourceTask(RESOURCE_GAZELLE,landIdleFarmer,huttingFarmer,NUMBER_HUTTINGFARMER);

    for(tagFarmer&farmer:huttingFarmer){
        for(tagResource&resource:info.resources){
            if(farmer.WorkObjectSN==resource.SN){
                if(resource.Blood<=DEADBLOOD){
                    for(tagResource&resource:info.resources){
                        if(resource.Type==RESOURCE_GAZELLE&&resource.Blood>DEADBLOOD){
                            HumanAction(farmer.SN,resource.SN);
                            break;
                        }
                    }
                }
            }
        }
    }
    //是否有活的羚羊
    for(tagResource&resource:info.resources){
        if(resource.Type==RESOURCE_GAZELLE&&resource.Blood>DEADBLOOD){
            return;
        }
    }

    bool hasStock = false;
    for(const tagBuilding& building : info.buildings) {
        if(building.Type == BUILDING_STOCK) {
            for(const tagResource& resource : info.resources) {
                if(resource.Type == RESOURCE_GAZELLE &&
                   abs(resource.BlockUR - building.BlockUR) + abs(resource.BlockDR - building.BlockDR) <= 10) {
                    hasStock = true;
                    break;
                }
            }
        }
        if(hasStock) break;
    }

    if(!hasStock){
        if(info.Wood>BUILD_STOCK_WOOD){
            pair<int,int> goal = findBestPosition(BUILDING_STOCK,RESOURCE_GAZELLE);
            for(tagFarmer&farmer:huttingFarmer){
                    HumanBuild(farmer.SN,BUILDING_STOCK,goal.first,goal.second);
            }
        }
        else{
            for(tagFarmer&farmer:huttingFarmer){
                    int  goalSN = findNearestResource(farmer,RESOURCE_TREE);
                    HumanAction(farmer.SN,goalSN);
            }
        }
    }
    else{
        assignResourceTask(RESOURCE_GAZELLE,landIdleFarmer,huttingFarmer,NUMBER_HUTTINGFARMER+1);
    }
}
void UsrAI::createFarmer(){
    for(tagBuilding&building:info.buildings){
        if(building.Type==BUILDING_CENTER){
            if(info.Meat>=BUILDING_CENTER_CREATEFARMER_FOOD&&building.Project==ACT_NULL&&info.farmers.size()<NUMBER_FARMER){
                BuildingAction(building.SN,BUILDING_CENTER_CREATEFARMER);
                break;
            }
        }
    }
}
void UsrAI::createSailing(){
    for(tagBuilding&building:info.buildings){
        if(building.Type==BUILDING_DOCK){
            if(info.Wood>=BUILDING_DOCK_CREATE_SAILING_WOOD&&building.Project==ACT_NULL&&getNumberFarmer(2)<NUMBER_SAILING){
                BuildingAction(building.SN,BUILDING_DOCK_CREATE_SAILING);
                break;
            }
        }
    }
}
void UsrAI::createBoat(){
    for(tagBuilding&building:info.buildings){
        if(building.Type==BUILDING_DOCK){
            if(info.Wood>=BUILDING_DOCK_CREATE_WOOD_BOAT_WOOD&&building.Project==ACT_NULL&&getNumberFarmer(1)<NUMBER_BOAT){
                BuildingAction(building.SN,BUILDING_DOCK_CREATE_WOOD_BOAT);
                break;
            }
        }
    }
}
void UsrAI::createShip(){
    for(tagBuilding&building:info.buildings){
        if(building.Type==BUILDING_DOCK){
            if(info.Wood>=BUILDING_DOCK_CREATE_SHIP_WOOD&&building.Project==ACT_NULL&&getNumberArmy(AT_SHIP)<NUMBER_SHIP){
                BuildingAction(building.SN,BUILDING_DOCK_CREATE_SHIP);
                break;
            }
        }
    }
}
void UsrAI::createClubman(){
    for(tagBuilding&building:info.buildings){
        if(building.Type==BUILDING_ARMYCAMP){
            if(info.Meat>=BUILDING_ARMYCAMP_CREATE_CLUBMAN_FOOD&&building.Project==ACT_NULL&&getNumberArmy(AT_CLUBMAN)<NUMBER_CLUBMAN){
                BuildingAction(building.SN,BUILDING_ARMYCAMP_CREATE_CLUBMAN);
                break;
            }
        }
    }
}
void UsrAI::createSlinger(){
    for(tagBuilding&building:info.buildings){
        if(building.Type==BUILDING_ARMYCAMP){
            if(info.Meat>=BUILDING_ARMYCAMP_CREATE_SLINGER_FOOD&&info.Stone>=BUILDING_ARMYCAMP_CREATE_SLINGER_STONE&&building.Project==ACT_NULL&&getNumberArmy(AT_SLINGER)<NUMBER_SLINGER){
                BuildingAction(building.SN,BUILDING_ARMYCAMP_CREATE_SLINGER);
                break;
            }
        }
    }
}
void UsrAI::createBowman(){
    for(tagBuilding&building:info.buildings){
        if(building.Type==BUILDING_RANGE){
            if(info.Meat>=BUILDING_RANGE_CREATE_BOWMAN_FOOD&&info.Wood>=BUILDING_RANGE_CREATE_BOWMAN_WOOD&&building.Project==ACT_NULL&&getNumberArmy(AT_BOWMAN)<NUMBER_BOWMAN){
                BuildingAction(building.SN,BUILDING_RANGE_CREATE_BOWMAN);
                break;
            }
        }
    }
}
void UsrAI::createScout(){
    for(tagBuilding&building:info.buildings){
        if(building.Type==BUILDING_STABLE){
            if(info.Meat>=BUILDING_STABLE_CREATE_SCOUT_FOOD&&building.Project==ACT_NULL&&getNumberArmy(AT_SCOUT)<NUMBER_SCOUT){
                BuildingAction(building.SN,BUILDING_STABLE_CREATE_SCOUT);
                break;
            }
        }
    }
}
void printMap(){
    for(int i=0;i<128;i++){
        for(int j=0;j<128;j++){
            cout<<myMap[i][j];
        }
        cout<<endl;
    }
}
void UsrAI::destroyFish(){
    for(tagFarmer&farmer:info.farmers){
        if(farmer.FarmerSort==2&&farmer.Resource==0){
            HumanAction(farmer.SN,farmer.SN);
        }
    }
}
void UsrAI::destroyShip(){
    for(tagArmy&army:info.armies){
        if(army.Sort==AT_SHIP){
            HumanAction(army.SN,army.SN);
        }
    }
}
void UsrAI::exploreEnermyLand(tagHuman& human) {
    bool visit[128][128];
    memset(visit,0,sizeof(visit));
    queue<pair<int, int>> q;
    pair<int,int> human_pos = {human.BlockDR, human.BlockUR};
    q.push(human_pos);
    visit[human.BlockDR][human.BlockUR] = true;

    while (!q.empty()) {
        pair<int,int> cur = q.front();
        q.pop();
        int x = cur.first, y = cur.second;

        if (myMap[x][y] == '?') {
            bool nearEnermyLand = false;
            for(int dx=-2;dx<=2;dx++){
                for(int dy=-2;dy<=2;dy++){
                    int nx = dx + x;
                    int ny = dy + y;
                    if(isValidCoordinate(nx,ny)&&isEnermyLand(nx,ny))
                        nearEnermyLand = true;
                }
                if(nearEnermyLand) break;
            }
            if(nearEnermyLand){
                DebugText("BFS探索敌方大陆未知区域");
                HumanMove(human.SN, x * BLOCKSIDELENGTH, y * BLOCKSIDELENGTH);
                return;
            }
        }

        for (auto dir : directions) {
            int nx = x + dir.first, ny = y + dir.second;
            if (isValidCoordinate(nx, ny) && !visit[nx][ny]) {
                // 只走陆地或未知格
                if (isEnermyLand(nx, ny) || myMap[nx][ny] == '?') {
                    visit[nx][ny] = true;
                    q.push({nx, ny});
                }
            }
        }
    }
}
int getAroundOcean(int DR,int UR){
    int count = 0;
    for(auto p:directions){
        int nx = DR + p.first;
        int ny = UR + p.second;
        if(isValidCoordinate(nx,ny)&&isOcean(nx,ny)) count++;
    }
    return count;
}
int getAroundLand(int DR,int UR){
    int count = 0;
    for(auto p:directions){
        int nx = DR + p.first;
        int ny = UR + p.second;
        if(isValidCoordinate(nx,ny)&&isLand(nx,ny)) count++;
    }
    return count;
}
void UsrAI::exploreOcean(tagHuman& human) {
    pair<int, int> human_pos = { human.BlockDR, human.BlockUR };
    
    bool nearEnemyLand = false;
    
    // 检查是否在敌方大陆附近
    for (int dx = -2; dx <= 2; dx++) {
        for (int dy = -2; dy <= 2; dy++) {
            int nx = human_pos.first + dx;
            int ny = human_pos.second + dy;
            if (isValidCoordinate(nx, ny) && isEnermyLand(nx, ny)) {
                nearEnemyLand = true;
                break;
            }
        }
        if (nearEnemyLand) break;
    }

        bool visit[128][128];
        memset(visit, 0, sizeof(visit));
        queue<pair<int, int>> q;
        pair<int, int> res = { -1, -1 };
        visit[human_pos.first][human_pos.second] = 1;
        q.push(human_pos);

        while (!q.empty()) {
            pair<int, int> current = q.front();
            q.pop();
            int x = current.first;
            int y = current.second;

            if (myMap[x][y] == '?') {
                bool likelyOcean = ((getAroundOcean(x,y)>0)&&(getAroundLand(x,y)==0));
                if(likelyOcean){
                    res = current;
                    break;
                }
            }

            // 扩展搜索范围
            for (const auto& p : directions) {
                int nx = x + p.first;
                int ny = y + p.second;
                if (isValidCoordinate(nx, ny) && !visit[nx][ny]) {
                    if (myMap[nx][ny] == '-' || myMap[nx][ny] == '?') {
                        q.push({ nx, ny });
                        visit[nx][ny] = 1;
                    }
                }
            }
        }

        if (res.first != -1 && res.second != -1) {
            DebugText("战船探索未知区域");
            HumanMove(human.SN, res.first * BLOCKSIDELENGTH, res.second * BLOCKSIDELENGTH);
            return;
        }
    
    // 最后：如果没有其他选择，随机移动到海洋区域
    for (auto p : directions) {
        int nx = human.BlockDR + p.first;
        int ny = human.BlockUR + p.second;
        if (isValidCoordinate(nx, ny) && isOcean(nx, ny)) {
            DebugText("战船随机移动到海洋区域");
            HumanMove(human.SN, nx * BLOCKSIDELENGTH, ny * BLOCKSIDELENGTH);
            return;
        }
    }
}
bool isFullOcean(int DR,int UR){
    for(int dx=-2;dx<=2;dx++){
        for(int dy=-2;dy<=2;dy++){
            int nx = DR + dx;
            int ny = UR + dy;
            if(isValidCoordinate(nx,ny)&&isLand(nx,ny)) return false;
        }
    }
    return true;
}
void UsrAI::assignOceanTask(){
    int enemySN = -1;
    for(tagArmy&army:info.enemy_armies){
        enemySN = army.SN;
        break;
    }
    if(enemySN!=-1){
        for(tagArmy&army:info.armies){
            if(army.NowState!=HUMAN_STATE_ATTACKING){
                HumanAction(army.SN,enemySN);
            }
        }
        return;
    }
    if(makeOceanWar){
        for(tagArmy&army:armyShip){
            if(army.NowState==HUMAN_STATE_IDLE){
                exploreOcean(army);
            }
            else if(army.NowState==HUMAN_STATE_WALKING){
                if(getNumberEnermyCoast()>=120) continue;
                if(getAroundLand(army.DR0/BLOCKSIDELENGTH,army.UR0/BLOCKSIDELENGTH)>0){
                    for(int dx = -5;dx<=5;dx++){
                        for(int dy = -5;dy<=5;dy++){
                            int nx = dx + army.BlockDR;
                            int ny = dy + army.BlockUR;
                            if(isFullOcean(nx,ny)){
                                HumanMove(army.SN,nx*BLOCKSIDELENGTH,ny*BLOCKSIDELENGTH);
                            }
                        }
                    }
                }
            }
        }
    }
}
void goToNextStage(){
    switch (stage) {
        case 1:
            if(info.civilizationStage == CIVILIZATION_TOOLAGE){
                stage++;
                // DebugText("Stage 1 -> Stage 2: Upgraded to Tool Age");
            }
            break;
        case 2: {
            int count = getNumberEnermyCoast();
            bool hasEnermyShip = false;
            for(tagArmy&army:info.enemy_armies){
                if(army.Sort==AT_SHIP){
                    hasEnermyShip = true;
                }
            }
            if(!hasEnermyShip&&count>120){
                stage++;
                // DebugText("Stage 2 -> Stage 3: No enemy ships and sufficient enemy coast");
            }
            break;
        }
        case 3:
        {
            int countGold = 0;
            for(tagResource&resource:info.resources){
                if(resource.Type==RESOURCE_GOLD){
                    countGold++;
                }
            }
            bool hasEnemy = false;
            for(tagArmy&army:info.enemy_armies){
                hasEnemy = true;
                break;
            }
            if(!hasEnemy&&countGold>=13){
                stage++;
            }
            break;
        }
    }
}
void UsrAI::updateScience(){
    for(tagBuilding&building:info.buildings){
        if(building.Percent<100) continue;
        switch (building.Type){
        case BUILDING_MARKET:
            if(!hasWoodScience&&building.Project==ACT_NULL&&info.Wood>=BUILDING_MARKET_WOOD_UPGRADE_WOOD&&info.Meat>=BUILDING_MARKET_WOOD_UPGRADE_FOOD){
                BuildingAction(building.SN,BUILDING_MARKET_WOOD_UPGRADE);
                hasWoodScience = true;
                DebugText("伐木科技已升级");
                return;
             }
             if(!hasFarmScience&&building.Project==ACT_NULL&&info.Wood>=BUILDING_MARKET_FARM_UPGRADE_WOOD&&info.Meat>=BUILDING_MARKET_FARM_UPGRADE_FOOD){
                 BuildingAction(building.SN,BUILDING_MARKET_FARM_UPGRADE);
                 hasFarmScience = true;
                 DebugText("农田科技已升级");
                 return;
             }
             if(!hasGoldScience&&building.Project==ACT_NULL&&info.Wood>=BUILDING_MARKET_GOLD_UPGRADE_WOOD&&info.Meat>=BUILDING_MARKET_GOLD_UPGRADE_FOOD&&stage==4){
                 BuildingAction(building.SN,BUILDING_MARKET_GOLD_UPGRADE);
                 hasGoldScience = true;
                 DebugText("炼金科技已升级");
                 return;
             }
            break;
        case BUILDING_STOCK:
            if(!hasUsetoolScience&&building.Project==ACT_NULL&&info.Meat>=BUILDING_STOCK_UPGRADE_USETOOL_FOOD){
                BuildingAction(building.SN,BUILDING_STOCK_UPGRADE_USETOOL);
                hasUsetoolScience = true;
                DebugText("工具利用科技已升级");
                return;
            }
            if(!hasDefenseInScience&&building.Project==ACT_NULL&&info.Meat>=BUILDING_STOCK_UPGRADE_DEFENSE_INFANTRY_FOOD){
                BuildingAction(building.SN,BUILDING_STOCK_UPGRADE_DEFENSE_INFANTRY);
                hasDefenseInScience = true;
                DebugText("in防守科技已升级");
                return;
            }
            if(!hasDefenseArScience&&building.Project==ACT_NULL&&info.Meat>=BUILDING_STOCK_UPGRADE_DEFENSE_ARCHER_FOOD){
                BuildingAction(building.SN,BUILDING_STOCK_UPGRADE_DEFENSE_ARCHER);
                hasDefenseArScience = true;
                DebugText("ar防守科技已升级");
                return;
            }
            if(!hasDefenseRiScience&&building.Project==ACT_NULL&&info.Meat>=BUILDING_STOCK_UPGRADE_DEFENSE_RIDER_FOOD){
                BuildingAction(building.SN,BUILDING_STOCK_UPGRADE_DEFENSE_RIDER);
                hasDefenseRiScience = true;
                DebugText("防守ri科技已升级");
                return;
            }
            break;
        case BUILDING_ARMYCAMP:
            if(!hasClubmanScience&&building.Project==ACT_NULL&&info.Meat>=BUILDING_ARMYCAMP_UPGRADE_CLUBMAN_FOOD){
                BuildingAction(building.SN,BUILDING_ARMYCAMP_UPGRADE_CLUBMAN);
                hasClubmanScience = true;
                DebugText("斧头兵科技已升级");
                return;
            }
            break;
        default:
            continue;
        }
    }
}
bool isPreBuild(int BUILDING_TYPE){
    // 检查是否有前置建筑要求
    auto it = Type_to_PreType.find(BUILDING_TYPE);
    if(it != Type_to_PreType.end()){
        int requiredBuilding = it->second;
        for(tagBuilding&building:info.buildings){
            if(building.Type==requiredBuilding&&building.Percent==100){
                return true;
            }

        }
    }
    else{
        return true;
    }
    return false;
}
void UsrAI::assignBoardTask(vector<tagFarmer>& IdleFarmer,vector<int>& boardFarmerSN){
    for(tagFarmer&boat:boatFarmer){
        if(boat.NowState!=HUMAN_STATE_IDLE) continue;
        if(!isNearCenter(boat.BlockDR,boat.BlockUR)) continue;
        while(!IdleFarmer.empty() && boardFarmerSN.size()+getNumberFarmer(0,true)<2){
            tagFarmer farmer = IdleFarmer.back();
            IdleFarmer.pop_back();
            boardFarmerSN.push_back(farmer.SN);
            HumanAction(farmer.SN,boat.SN);
        }
    }
}
bool isAllBoard(vector<int>& boardFarmerSN){
    // 检查所有需要上船的村民是否都已经上船
    // 如果boardFarmerSN为空，说明没有村民需要上船
    if(boardFarmerSN.empty()) return true;
    
    // 检查是否还有村民在陆地上（未上船）
    for(int&farmerSN:boardFarmerSN){
        bool foundOnLand = false;
        for(tagFarmer&farmer:info.farmers){
            if(farmerSN==farmer.SN){
                foundOnLand = true;
                break;
            }
        }
        // 如果找到村民还在陆地上，说明还没全部上船
        if(foundOnLand) return false;
    }
    // 所有村民都不在陆地上，说明都已经上船
    return true;
}
bool isAllDisembark(vector<int>& boardFarmerSN){
    if(boardFarmerSN.empty()) return true;
    for(int&farmerSN:boardFarmerSN){
        bool isDisembark = false;
        for(tagFarmer&farmer:info.farmers){
            if(farmerSN==farmer.SN){
                isDisembark = true;
                break;
            }
        }
        if(!isDisembark) return false;
    }
    boardFarmerSN.clear();
    return true;
}
void UsrAI::assignTransportTask(){
    //没有船，无需分配运输任务
    if(boatFarmer.empty()) return;
    //寻找上船下船点
    if(boardPos.first==-1) boardPos = findBoardPos();
    if(landPos.first==-1) landPos = findLandPos();
    if(boardPos.first==-1||landPos.first==-1) return;
    //敌方大陆人数未达需求需要分配上船任务
    if(getNumberFarmer(0,true)+boardFarmerSN.size()<2){
        assignBoardTask(landIdleFarmer,boardFarmerSN);
        return;
    }
    for(tagFarmer&boat:boatFarmer){
        if(boat.NowState==HUMAN_STATE_IDLE){
            bool isNearLand = (getAroundLand(boat.BlockDR,boat.BlockUR)>0);
            if(isNearCenter(boat.BlockDR,boat.BlockUR)){
                DebugText("距离城镇中心近");
                //没有上船人员，不做操作
                if(boardFarmerSN.empty()) continue;
                if(isAllBoard(boardFarmerSN)){
                    if(isNearLand){
                        DebugText("船只离开岸边，向海洋行驶");
                        for(int dx=-5;dx<=5;dx++){
                            for(int dy=-5;dy<=5;dy++){
                                int nx = boat.BlockDR+dx;
                                int ny = boat.BlockUR+dy;
                                if(isValidCoordinate(nx,ny)&&isFullOcean(nx,ny)){
                                    HumanMove(boat.SN,nx*BLOCKSIDELENGTH,ny*BLOCKSIDELENGTH);
                                    return;
                                }
                            }
                        }
                    }
                    DebugText("船只离开岸边,驶往下船坐标");
                    HumanMove(boat.SN,landPos.first*BLOCKSIDELENGTH,landPos.second*BLOCKSIDELENGTH);
                    return;
                }
                else{
                    DebugText("还有村民未上船");
                    if(isNearLand){
                        DebugText("船已停靠在岸边");
                        return;
                    }
                    else{
                        DebugText("向预定上船位置移动");
                        HumanMove(boat.SN,boardPos.first*BLOCKSIDELENGTH,boardPos.second*BLOCKSIDELENGTH);
                        return;
                    }
                }
            }
            else{
                DebugText("距离城镇中心远");
                if(isAllDisembark(boardFarmerSN)){
                    DebugText("所有村民都已下船，返回上船位置");
                    HumanMove(boat.SN,boardPos.first*BLOCKSIDELENGTH,boardPos.second*BLOCKSIDELENGTH);
                    return;
                }
                else{
                    DebugText("还有村民未下船");
                    if(isNearLand){
                        DebugText("在陆地旁");
                        frameCount++;
                        if(frameCount==3){
                            DebugText("向附近陆地靠近");
                            for(auto p:directions){
                                int nx = p.first + boat.BlockDR;
                                int ny = p.second+ boat.BlockUR;
                                if(isEnermyLand(nx,ny)){
                                    HumanMove(boat.SN,boat.BlockDR*BLOCKSIDELENGTH+p.first*3,boat.BlockUR*BLOCKSIDELENGTH+p.second*3);
                                    frameCount=0;
                                    return;
                                }
                            }
                        }
                    }
                }
            }
        }
        else if(boat.NowState==HUMAN_STATE_WALKING){
            if(boat.BlockDR==landPos.first&&boat.BlockUR==landPos.second){
                if(boat.DR0==landPos.first*BLOCKSIDELENGTH&&boat.UR0==landPos.second*BLOCKSIDELENGTH){
                    DebugText("到达下船坐标，停船");
                    HumanMove(boat.SN,boat.DR,boat.UR);
                }
            }
        }
    }
}
bool isNearCenter(int DR,int UR){
    int standard = abs(63-CENTER.BlockDR)+abs(63-CENTER.BlockUR);
    int dis = abs(DR-CENTER.BlockDR)+abs(UR-CENTER.BlockUR);
    if(dis>=standard) return false;
    else return true;
}
pair<int,int> findBoardPos(){
    pair<int,int> res = {-1,-1};
    if(!boatFarmer.size()) return res;
    tagFarmer&boat = boatFarmer.back();
    queue<pair<int,int>> q;
    bool visit[128][128];
    memset(visit, 0, sizeof(visit));  // 初始化visit数组
    int x = boat.BlockDR;
    int y = boat.BlockUR;
    
    // 检查船的初始位置是否有效
    if(!isValidCoordinate(x, y)) {
        return res;
    }
    
    visit[x][y]=true;
    q.push({x,y});
    while(!q.empty()){
        pair<int,int> cur = q.front();
        q.pop();
        if(isCoast(cur.first,cur.second)){
           res = cur;
           break;
        }
        for(auto dir:directions){
            int nx = cur.first+dir.first;
            int ny = cur.second+dir.second;
            // 添加边界检查
            if(isValidCoordinate(nx,ny) && !visit[nx][ny]){
                q.push({nx,ny});
                visit[nx][ny]=true;
            }
        }
    }
    return res;
}
bool isCoast(int DR,int UR){
    if(!isLand(DR,UR)) return false;
    
    // 检查四个方向是否有海洋
    int oceanCount = 0;
    for(auto dir:directions){
        int nx = DR+dir.first;
        int ny = UR+dir.second;
        // 添加边界检查
        if(isValidCoordinate(nx,ny) && isOcean(nx,ny)){
            oceanCount++;
        }
    }
    
    // 至少有一个方向是海洋，且不是完全被海洋包围
    return oceanCount > 0 && oceanCount < 4;
}
int getNumberEnermyCoast(){
    int count = 0;
    for(int i=0;i<128;i++){
        for(int j=0;j<128;j++){
            // 使用安全的坐标检查
            if(isValidCoordinate(i,j) && isEnermyLand(i,j)&&isCoast(i,j)) count++;
        }
    }
    return count;
}
bool isValidCoordinate(int x, int y) {
    return x >= 0 && x < 128 && y >= 0 && y < 128;
}
void UsrAI::assignEnemyLandTask(){
    int enemySN = -1 ;
    for(tagArmy&army:info.enemy_armies){
        enemySN = army.SN;
        break;
    }
    if(enemySN!=-1){
        for(tagArmy&army:info.armies){
            if(army.NowState==HUMAN_STATE_IDLE){
                HumanAction(army.SN,enemySN);
            }
        }
        return;
    }
    if(makeLandWar){
        //发动战争了
        for(tagArmy&army:info.armies){
            if(army.NowState==HUMAN_STATE_IDLE){
                exploreEnermyLand(army);
            }
        }
    }
    else{
        //未发动战争时，探索附近的视野
        if(!enermyLandIdleFarmer.empty()){
            assignBuildTask(BUILDING_ARMYCAMP,enermyLandIdleFarmer,buildingArmyCampFarmer,NUMBER_BUILDINGFARMER_ARMYCAMP,findBuildingBlock(BUILDING_ARMYCAMP,true),BUILD_ARMYCAMP_WOOD,NUMBER_ARMYCAMP);
            assignBuildTask(BUILDING_STABLE,enermyLandIdleFarmer,buildingStableFarmer,NUMBER_BUILDINGFARMER_STABLE,findBuildingBlock(BUILDING_STABLE,true),BUILD_STABLE_WOOD,NUMBER_STABLE);
            assignBuildTask(BUILDING_RANGE,enermyLandIdleFarmer,buildingRangeFarmer,NUMBER_BUILDINGFARMER_RANGE,findBuildingBlock(BUILDING_RANGE,true),BUILD_RANGE_WOOD,NUMBER_RANGE);
            for(tagFarmer&farmer:enermyLandIdleFarmer){
                int dis = abs(farmer.BlockDR-landPos.first)+abs(farmer.BlockUR-landPos.second);
                if(dis<=15){
                    exploreEnermyLand(farmer);
                }
            }
        }
        for(tagArmy&army:info.armies){
            int dis = abs(army.BlockDR-landPos.first)+abs(army.BlockUR-landPos.second);
            if(dis<=15&&army.NowState==HUMAN_STATE_IDLE&&army.Sort!=AT_SHIP){
                exploreEnermyLand(army);
            }
        }
    }
}
/*##########MY CODE ENDS HERE##########*/
void UsrAI::processData()
{
    info = getInfo();
    init();
    goToNextStage();
    printMap();
    switch (stage) {
    case 1:
        //时代升级中
        if(CENTER.Project==BUILDING_CENTER_UPGRADE){
            // 建谷仓
            assignBuildTask(BUILDING_GRANARY,landIdleFarmer,buildingGranaryFarmer,NUMBER_BUILDINGFARMER_GRANARY,findBuildingBlock(BUILDING_GRANARY),BUILD_GRANARY_WOOD,NUMBER_GANARY);
            // 建船坞
            assignBuildTask(BUILDING_DOCK,landIdleFarmer,buildingDockFarmer,NUMBER_BUILDINGFARMER_DOCK,findBuildingBlock(BUILDING_DOCK),BUILD_DOCK_WOOD,NUMBER_DOCK);
            //打猎
            huttingGazelle();
            // 砍树
            assignResourceTask(RESOURCE_TREE,landIdleFarmer,cuttingFarmer,NUMBER_CUTTINGFARMER);
            //捕鱼
            assignResourceTask(RESOURCE_FISH,waterIdleFarmer,fishingFarmer,NUMBER_FISHINGFARMER);
        }
        else{
            //石器时代
            if(landIdleFarmer.size()||waterIdleFarmer.size()){
                // assign building task

                  //build home
                  assignBuildTask(BUILDING_HOME,landIdleFarmer,buildingHomeFarmer,NUMBER_BUILDINGFARMER_HOME,findBuildingBlock(BUILDING_HOME),BUILD_HOUSE_WOOD,NUMBER_HOME);

                  //build dock
                  assignBuildTask(BUILDING_DOCK,landIdleFarmer,buildingDockFarmer,NUMBER_BUILDINGFARMER_DOCK,findBestPosition(BUILDING_DOCK,RESOURCE_FISH),BUILD_DOCK_WOOD);

                  //build stock
                  assignBuildTask(BUILDING_STOCK,landIdleFarmer,buildingStockFarmer,NUMBER_BUILDINGFARMER_STOCK,findBestPosition(BUILDING_STOCK,RESOURCE_TREE),BUILD_STOCK_WOOD,NUMBER_STOCK);
                //assign resource task

                  //hutting
                  huttingGazelle();

                  //cutting
                  assignResourceTask(RESOURCE_TREE,landIdleFarmer,cuttingFarmer,NUMBER_CUTTINGFARMER);

                  //fishing
                  assignResourceTask(RESOURCE_FISH,waterIdleFarmer,fishingFarmer,NUMBER_FISHINGFARMER);


                // building action

                  //creat sailings
                  createSailing();

                  //creat farmers
                  createFarmer();
            }
             if(info.farmers.size()==NUMBER_FARMER&&info.Meat>=BUILDING_CENTER_UPGRADE_FOOD&&info.civilizationStage==CIVILIZATION_STONEAGE){
                 BuildingAction(CENTER.SN,BUILDING_CENTER_UPGRADE);
             }
        }
        break;
    case 2:

        updateScience();
        destroyFish();
        createShip();
        // build market
        assignBuildTask(BUILDING_MARKET,landIdleFarmer,buildingMarketFarmer,NUMBER_BUILDINGFARMER_MARKET,findBuildingBlock(BUILDING_MARKET),BUILD_MARKET_WOOD,NUMBER_MARKET);
        // build farm
        assignBuildTask(BUILDING_FARM,landIdleFarmer,buildingFarmFarmer,NUMBER_BUILDINGFARMER_FARM,findBuildingBlock(BUILDING_FARM),BUILD_FARM_WOOD,NUMBER_FARM);
        assignBuildTask(BUILDING_HOME,landIdleFarmer,buildingHomeFarmer,NUMBER_BUILDINGFARMER_HOME,findBuildingBlock(BUILDING_HOME),BUILD_HOUSE_WOOD,NUMBER_HOME+2);
        assignResourceTask(RESOURCE_TREE,landIdleFarmer,cuttingFarmer,NUMBER_CUTTINGFARMER);  
        assignOceanTask();
        break;
    case 3:
        cout<<"landPos:"<<landPos.first<<' '<<landPos.second<<endl;
        assignOceanTask();
        createBoat();
        createBowman();
        createSlinger();
        createClubman();
        createScout();
        assignTransportTask();
        assignBuildTask(BUILDING_FARM,landIdleFarmer,buildingFarmFarmer,NUMBER_BUILDINGFARMER_FARM,findBuildingBlock(BUILDING_FARM),BUILD_FARM_WOOD,NUMBER_FARM);
        assignResourceTask(RESOURCE_TREE,landIdleFarmer,cuttingFarmer,NUMBER_CUTTINGFARMER-2);
        assignEnemyLandTask();
        updateScience();
        break;
    case 4:
        for(tagFarmer&farmer:info.farmers){
            if(farmer.NowState==HUMAN_STATE_WORKING&&isNearCenter(farmer.BlockDR,farmer.BlockUR)){
                HumanMove(farmer.SN,farmer.BlockDR*BLOCKSIDELENGTH,farmer.BlockUR*BLOCKSIDELENGTH);
            }
        }
        assignTransportTask();
        updateScience();
        assignBuildTask(BUILDING_STOCK,enermyLandIdleFarmer,buildingStockFarmer,NUMBER_BUILDINGFARMER_STOCK,findBestPosition(BUILDING_STOCK,RESOURCE_GOLD,true),BUILD_STOCK_WOOD,NUMBER_STOCK+2);
        assignResourceTask(RESOURCE_GOLD,enermyLandIdleFarmer,diggingGoldFarmer,enermyLandIdleFarmer.size(),true);
        break;
    }
}


